import 'package:flutter/widgets.dart';
import 'package:library_base/entities/goods_child_entity.dart';
import 'package:library_base/entities/goods_entity.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:library_base/log/fb_log.dart';
import 'package:library_base/net/apis.dart';
import 'package:library_base/network/http_config.dart';
import 'package:get/get.dart';
import 'package:library_base/provider/base_list_controller.dart';
import 'package:library_base/utils/toast_util.dart';
import 'package:library_base/widget/multi_state_widget/multi_state_widget.dart';

class GoodsListController extends BaseListController<GoodsEntity, GoodsChildEntity> {
  final TextEditingController keyWorkSearchController = TextEditingController();

  /// 实体类
  RxList<GoodsChildEntity> listDataSelected = <GoodsChildEntity>[].obs;

  List<GoodsChildEntity> initlistDataSelected = <GoodsChildEntity>[];

  Rx<String> keyWord = ''.obs;

  Rx<bool> isAction = false.obs;

  late String shipperId;

  @override
  void onCreate() {
    super.onCreate();
    var argumentMap = Get.arguments as Map<String, dynamic>;

    shipperId = argumentMap['shipperId'] != null ? argumentMap['shipperId'] as String : '';

    var list = argumentMap['GoodsChildEntities'] != null
        ? argumentMap['GoodsChildEntities'] as List<GoodsChildEntity>
        : [];

    listDataSelected.value = List<GoodsChildEntity>.generate(list.length, (int index) {
      return list[index];
    }, growable: true);

    initlistDataSelected =
        List<GoodsChildEntity>.generate(listDataSelected.value.length, (int index) {
      return listDataSelected.value[index];
    }, growable: true);

    logger.i('测试对象新页面的复制后 code${initlistDataSelected.hashCode}');
  }

  @override
  Future requestData() {
    isFirstLoading = true;
    requestGeneralListData(Apis.GOODS, isRefresh: false, method: FBResultFul.GET, queryParameters: {
      "pageSize": '$limit',
      "pageNo": '${getPage(false)}',
      'goodsName': keyWorkSearchController.text,
      'shipperId': shipperId,
    });
    return super.requestData();
  }

  @override
  List<GoodsChildEntity> transformObject(GoodsEntity data, bool isRefresh) {
    for (var element in listDataSelected.value) {
      for (var record in data.records) {
        if (record == element) {
          record.isSelect = true;
        }
      }
    }
    return data.records;
  }

  @override
  Future onRefreshCallback() async {
    requestGeneralListData(Apis.GOODS, isRefresh: true, method: FBResultFul.GET, queryParameters: {
      "pageSize": '$limit',
      "pageNo": '${getPage(true)}',
      'goodsName': keyWorkSearchController.text,
      'shipperId': shipperId,
    });
  }

  @override
  Future onLoadCallback() async {
    requestGeneralListData(Apis.GOODS, isRefresh: false, method: FBResultFul.GET, queryParameters: {
      "pageSize": '$limit',
      "pageNo": '${getPage(false)}',
      'goodsName': keyWorkSearchController.text,
      'shipperId': shipperId,
    });
  }

  void onSearch(String action) {
    if (keyWorkSearchController.text.isEmpty) {
      ToastUtil.waring(MultiLanguage.pleaseEnterProductName.tr);
      return;
    }
    isFirstLoading = true;
    multiControllerOut.viewStateChange(MultiStateStatus.loading);
    requestGeneralListData(Apis.GOODS, isRefresh: false, method: FBResultFul.GET, queryParameters: {
      "pageSize": '$limit',
      "pageNo": '${getPage(false)}',
      'goodsName': keyWorkSearchController.text,
      'shipperId': shipperId,
    });
  }

  void backToList() {
    keyWorkSearchController.text = "";
    keyWord.value = "";
    isFirstLoading = true;
    multiControllerOut.viewStateChange(MultiStateStatus.loading);
    requestGeneralListData(Apis.GOODS, isRefresh: false, method: FBResultFul.GET, queryParameters: {
      "pageSize": '$limit',
      "pageNo": '${getPage(false)}',
      'goodsName': keyWorkSearchController.text,
      'shipperId': shipperId,
    });
  }

  void tryReLoad() {
    isFirstLoading = true;
    multiControllerOut.viewStateChange(MultiStateStatus.loading);
    baseState?.setStateCallBack?.call();
    requestGeneralListData(Apis.GOODS,
        isRefresh: false,
        method: FBResultFul.GET,
        params: {},
        queryParameters: {
          "pageSize": '$limit',
          "pageNo": '${getPage(false)}',
          'goodsName': keyWorkSearchController.text,
          'shipperId': shipperId,
        });
  }

  void onItemClick(GoodsChildEntity itemData) {
    itemData.isSelect = !itemData.isSelect;
    if (!listDataSelected.value.contains(itemData)) {
      listDataSelected.value.add(itemData);
    } else {
      listDataSelected.value.remove(itemData);
    }
    update();
  }
}
